local skill = fk.CreateSkill{
  name = "tea__guijian",
}
local U = require "packages/utility/utility"
local getDiscardNum = function(player)
  return #player:getCardIds("h") - player.maxHp
end
---@class ViewAsSkill
---@field cost_data any
---@type ViewAsSkillSpec
local guijian = {
  anim_type = "special",
  pattern = ".|.|.|.|.|basic",
  mute = true,
  interaction = function(self, player)
    local all_names = Fk:getAllCardNames("b")
    local names = player:getViewAsCardNames("tea__guijian", all_names)
    if #names > 0 then
      return UI.CardNameBox { choices = names, all_choices = all_names }
    end
  end,
  card_filter = function(self, player, to_select, selected)
    return table.contains(Self:getCardIds("h"), to_select) and getDiscardNum(Self) > #selected
  end,
  view_as = function(self, player, cards)
    if not self.interaction.data then return end
    if getDiscardNum(player) <= 0 then
      if #cards > 0 then return end
    else
      if getDiscardNum(player) ~= #cards then return end
    end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = skill.name
    self.cost_data = cards --fixme
    return card
  end,
  before_use = function(self, player, use)
    local room = player.room
    local name = use.card.trueName
    room:notifySkillInvoked(player, "tea__guijian", "special")
    if name == "slash" then
      player:broadcastSkillInvoke("tea__guijian", 1)
    elseif name == "jink" then
      player:broadcastSkillInvoke("tea__guijian", 2)
    elseif name == "peach" then
      player:broadcastSkillInvoke("tea__guijian", 3)
    else
      player:broadcastSkillInvoke("tea__guijian", 4)
    end
    local cids = self.cost_data
    if #(cids or {}) > 0 then
      room:throwCard(cids, skill.name, player, player)
    end
    if getDiscardNum(player) < 0 then
      room:setPlayerMark(player, "@@tea__guijian_debuff-turn", 1)
      player:drawCards(math.abs(getDiscardNum(player)), skill.name)
    end
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(skill.name, Player.HistoryTurn) == 0
  end,
  enabled_at_response = function(self, player, response)
    return player:usedSkillTimes(skill.name, Player.HistoryTurn) == 0 and not response
  end,
}
---@type TrigSkelSpec<UseCardFunc>
local guijian_delay = {
  anim_type = "negative",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and not player:isNude() and player:getMark("@@tea__guijian_debuff-turn") > 0 and not table.contains(data.card.skillNames, skill.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player.room:askToDiscard(player, {
      min_num = 1,
      max_num = 1,
      include_equip = true,
      skill_name = skill.name,
      cancelable = false
    })
  end,
}
skill:addEffect("viewas", guijian)
skill:addEffect(fk.CardUsing, guijian_delay)
return skill